package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.ExperimentProject;
import com.ruoyi.business.param.LabCountXmlParam;
import com.ruoyi.business.service.IExperimentProjectService;
import com.ruoyi.business.vo.ExperimentProjectExportVo;
import com.ruoyi.business.vo.ExperimentTimeRangeVO;
import com.ruoyi.business.vo.LabInfoVo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 实验项目管理Controller
 *
 * @author qiancheng
 * @date 2025-06-13
 */
@Api(tags = "实验项目管理")
@RestController
@RequestMapping("/business/project")
public class ExperimentProjectController extends BaseController
{
    @Autowired
    private IExperimentProjectService experimentProjectService;

    /**
     * 实验平台管理->>实验项目管理->>查询实验项目管理列表
     */
    @ApiOperation("查询实验项目管理列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam("实验项目查询条件") ExperimentProject experimentProject)
    {
        startPage();
        List<ExperimentProject> list = experimentProjectService.selectExperimentProjectList(experimentProject);
        return getDataTable(list);
    }

    /**
     * 实验平台管理->>实验项目管理->>更多->>根据年月周查询实验次数
     */
    @ApiOperation("根据年月周查询实验次数")
    @Anonymous
    @GetMapping("/getCountByDate")
    public AjaxResult getCountByDate(@ApiParam("实验统计查询条件") LabCountXmlParam labCountXmlParam)
    {
        return  success(experimentProjectService.getCountByDate(labCountXmlParam));
    }

    /**
     * 实验平台管理->>实验项目管理->>更多->>根据年月周查询实验项目管理列表
     */
    @ApiOperation("根据年月周查询实验项目管理列表")
    @Anonymous
    @GetMapping("/getListByDate")
    public TableDataInfo getListByDate(@ApiParam("实验统计查询条件") LabCountXmlParam labCountXmlParam)
    {
        startPage();
        List<ExperimentProject> list = experimentProjectService.getListByDate(labCountXmlParam);
        return getDataTable(list);
    }

    /**
     * 实验平台管理->>实验项目管理->>更多->>导出数据->>导出年月周查询实验项目管理列表
     */
    @ApiOperation("导出年月周查询实验项目管理列表")
    @Anonymous
    @PostMapping("/getListByDateExport")
    public void getListByDateExport(HttpServletResponse response,@ApiParam("实验统计查询条件") LabCountXmlParam labCountXmlParam)
    {

        List<ExperimentProjectExportVo> list = experimentProjectService.getListByDateExport(labCountXmlParam);
        ExcelUtil<ExperimentProjectExportVo> util = new ExcelUtil<ExperimentProjectExportVo>(ExperimentProjectExportVo.class);
        util.exportExcel(response, list, "实验项目管理数据");
    }


    /**
     * 首页->>实验次数->>根据年月查询实验平台的实验次数
     */
    @ApiOperation("根据年月查询实验平台的实验次数")
    @Anonymous
    @GetMapping("/getCountByLab")
    public TableDataInfo getCountByLab(@ApiParam("实验项目查询条件") ExperimentProject experimentProject)
    {
        List<LabInfoVo> countByLab = experimentProjectService.getCountByLab(experimentProject);
        return getDataTable(countByLab);
    }

    /**
     * 导出实验项目管理列表
     */
    @ApiOperation("导出实验项目管理列表")
    @PreAuthorize("@ss.hasPermi('business:project:export')")
    @Log(title = "实验项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam("实验项目查询条件") ExperimentProject experimentProject)
    {
        List<ExperimentProject> list = experimentProjectService.selectExperimentProjectList(experimentProject);
        ExcelUtil<ExperimentProject> util = new ExcelUtil<ExperimentProject>(ExperimentProject.class);
        util.exportExcel(response, list, "实验项目管理数据");
    }

    /**
     * 获取实验项目管理详细信息
     */
    @ApiOperation("获取实验项目管理详细信息")
    @PreAuthorize("@ss.hasPermi('business:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("实验项目ID") @PathVariable("id") Long id)
    {
        return success(experimentProjectService.selectExperimentProjectById(id));
    }

    /**
     * 实验平台管理->>实验项目管理->>新增实验项目管理
     */
    @ApiOperation("新增实验项目管理")
    @RepeatSubmit
    @Anonymous
    @PostMapping
    public AjaxResult add(@ApiParam("文件") @RequestParam(value = "file", required = false) MultipartFile[] files,
                          @ApiParam("实验项目信息") @RequestParam("experimentProject") String experimentProject)
    {
        try {
            // 将JSON字符串转换为RackEquipment对象
            ExperimentProject exp = JSON.parseObject(experimentProject, ExperimentProject.class);

            return success(experimentProjectService.insertExperimentProject(files,exp));
        } catch (Exception e) {
            return error("新增实验项目失败：" + e.getMessage());
        }
    }

    /**
     * 实验平台管理->>实验项目管理->>修改实验项目管理
     */
    @ApiOperation("修改实验项目管理")
    @Anonymous
    @PutMapping
    public AjaxResult edit(@ApiParam("实验项目信息") @RequestBody ExperimentProject experimentProject)
    {
        return toAjax(experimentProjectService.updateExperimentProject(experimentProject));
    }

    /**
     * 实验平台管理->>实验项目管理->>删除实验项目管理
     */
    @ApiOperation("删除实验项目管理")
    @Anonymous
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("实验项目ID数组") @PathVariable Long[] ids)
    {
        return toAjax(experimentProjectService.deleteExperimentProjectByIds(ids));
    }

    /**
     * 首页->>燃烧实验平台->>根据平台获取所属实验项目列表
     */
    @Anonymous
    @GetMapping("/selectProjectsByLabId")
    public TableDataInfo selectProjectsByLabId(ExperimentProject experimentProject)
    {
        startPage();
        List<ExperimentProject> list = experimentProjectService.selectProjectsByLabId(experimentProject);
        return getDataTable(list);
    }


    /**
     * 获取实验时间列表
     */
    @Anonymous
    @GetMapping("/experimentTimes")
    public AjaxResult getExperimentTimes(@RequestParam("rackId") String rackId) {
        List<String> times = experimentProjectService.getExperimentTimeList(rackId);
        return AjaxResult.success(times);
    }


    /**
     * 根据实验日期查询当天实验的时间范围列表
     */
    @Anonymous
    @GetMapping("/experimentTimeRangesByDate")
    public AjaxResult getExperimentTimeRangesByDate(@RequestParam("experimentDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date experimentDate,
            @RequestParam("rackId") String rackId) {
        List<ExperimentTimeRangeVO> list = experimentProjectService.getExperimentTimeRangesByDate(experimentDate, rackId);
        return AjaxResult.success(list);
    }

}
